package myky.com.doctorapp.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import myky.com.doctorapp.R;
import myky.com.doctorapp.entity.LoginEntity;
import myky.com.doctorapp.entity.LoginSuccessEntity;
import myky.com.doctorapp.entity.NewConsultListBean;
import myky.com.doctorapp.base.BaseFragment;
import myky.com.doctorapp.contract.PwLoginFragmentContract;
import myky.com.doctorapp.entity.PageEntity;
import myky.com.doctorapp.presenter.PwLoginFragmentPresenter;
import myky.com.doctorapp.utils.CheckTextUtils;
import myky.com.doctorapp.utils.Constants;
import myky.com.doctorapp.utils.ShareUtil;
import myky.com.doctorapp.utils.SpanUtils;
import myky.com.doctorapp.utils.ToastUtils;
import myky.com.doctorapp.wight.GeneralEditText;

/**
 * @author Mr.li
 *
 * warming: 用户登陆成功后台没有返回用户信息
 */
public class PwLoginFragment extends BaseFragment<PwLoginFragmentPresenter, LoginEntity.PayLoadBean> implements PwLoginFragmentContract.View {
    @BindView(R.id.activity_login_user_et)
    GeneralEditText mActivityLoginUserEt;
    @BindView(R.id.activity_login_pw_tv)
    TextView mActivityLoginPwTv;
    @BindView(R.id.activity_login_pw_et)
    GeneralEditText mActivityLoginPwEt;
    @BindView(R.id.privacy_policy_tv)
    TextView mPrivacyPolicyTv;
    @BindView(R.id.login_password)
    LinearLayout mLoginPassword;
    @BindView(R.id.activity_login_button)
    Button mActivityLoginButton;

    private String user_name, pass_word;
    private Integer PW_MIN_LENGTH = 6;
    private Integer PW_MAX_LENGTH = 15;

    @Override
    public PwLoginFragmentPresenter getPresenter() {
        return new PwLoginFragmentPresenter();
    }

    @Override
    protected int injectTarget() {
        return R.id.rootView;
    }

    @Override
    protected void doRetry() {

    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fra_pwlogin;
    }

    @Override
    protected void initView() {
        SpanUtils spanUtils = new SpanUtils();
        spanUtils.append(getResources().getString(R.string.login_agreement))
                .append("《C麦隐私政策》")
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        ToastUtils.showAtCenter("跳转隐私政策");
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(getResources().getColor(R.color.theme_app));
                        ds.setUnderlineText(false);
                    }
                });
        mPrivacyPolicyTv.setMovementMethod(LinkMovementMethod.getInstance());
        mPrivacyPolicyTv.setText(spanUtils.create());
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void showDataError(String errorMessage) {
        ToastUtils.showAtCenter(errorMessage);
    }

    @Override
    public void showDataSuccess(LoginEntity.PayLoadBean datas) {

    }

    @Override
    public void showPages(PageEntity pageEntity) {

    }


    @Override
    public void loginFailed(String message) {

    }

    @Override
    public void loginSuccess(LoginSuccessEntity.PayLoadBean successEntity) {


        KeyboardUtils.hideSoftInput(getActivity());

        int userid = successEntity.getUser().getUserid();



        ShareUtil.putData(Constants.USERID,userid);

        Intent intent=new Intent();
        intent.putExtra("IS_LOGIN", "IS_LOGIN");
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }

    @Override
    public void vlist(List<NewConsultListBean.PayLoadBean.SpecialistConsultsBean> consultsBeanList) {

    }

    @OnClick({R.id.activity_login_button, R.id.activity_login_pw_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_login_button:
                user_name = mActivityLoginUserEt.getText().toString();
                pass_word = mActivityLoginPwEt.getText().toString();
                if (!CheckTextUtils.isUserName(user_name)) {
                    ToastUtils.show("账号需4-20个字符");
                    return;
                }
                if (pass_word.equals("") || pass_word.length() < PW_MIN_LENGTH || pass_word.length() > PW_MAX_LENGTH) {
                    ToastUtils.show("密码需6-15个字符");
                    return;
                }
                ShareUtil.putData(Constants.USERNAME, user_name);
                ShareUtil.putData(Constants.PASSWORD, pass_word);
                ShareUtil.putData("isQuickLogin",false);
                mPresenter.login();
                break;
            case R.id.activity_login_pw_tv:
                ToastUtils.showAtCenter("点击忘记密码");
                break;
            default:
        }
    }


}
